﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SdmxMl.Manager;
using SdmxMl.Multiplexor;

namespace SdmxMl.Panels
{
    /// <summary>
    /// Dialog to select an artefact backup for loading
    /// Autjor C.del.
    /// </summary>
    public partial class DlgBackupArtefact : Form
    {

        #region internal members
        private string _artefactType;
        private string _id;
        private string _agency;
        private string _version;
        private ProxyService _trp;
        private DataSet ds;

        private backupVersion _selectedBackup;

        #endregion

        #region public members
        public string ArtefactType
        {
            get { return (this._artefactType); }
            set { this._artefactType = value; }
        }
        public string Id
        {
            get { return (this._id); }
            set { this._id = value; }
        }
        public string Agency
        {
            get { return (this._agency); }
            set { this._agency = value; }
        }
        public string Version
        {
            get { return (this._version); }
            set { this._version = value; }
        }
        public ProxyService TinyRegistry
        {
            get { return (this._trp); }
            set { this._trp = value; }
        }
        public backupVersion SelectedBackup
        {
            get { return (this._selectedBackup); }
            set { this._selectedBackup = value; }
        }
        #endregion

        #region Constructor(s)
        public DlgBackupArtefact()
        {
            InitializeComponent();
        }
        #endregion

        #region public methods
        public void GetInformationBackup()
        {
            btnValide.Enabled = false;
            lbBackupVersion.Items.Clear();

            StringBuilder sb = new StringBuilder();
            sb.Append(_agency).Append("+").Append(_id).Append("+").Append(_version);
            ds = TinyRegistry.GetBackupList(_artefactType, sb.ToString());
            if (ds != null &&
                ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    btnValide.Enabled = true;
                    DataView dvBackup = new DataView(dt);
                    dvBackup.Sort = "VERSION_NUMBER DESC";
                    foreach (DataRow dr in dvBackup.Table.Rows)
                    {
                        backupVersion cur = new backupVersion();
                        cur.Agecny = _agency;
                        cur.ArtefactType = _artefactType;
                        cur.Id = _id;
                        cur.Version = dr["VERSION_NUMBER"].ToString();
                        cur.DateHeure = dr["VERSION_DATE"].ToString();
                        lbBackupVersion.Items.Add(cur);
                    }

                    lbBackupVersion.SelectedIndex = 0;
                }
            }
        }
            
        #endregion

        #region private methods

        #endregion

        #region Events definitions

        /// <summary> Event raised on Close button click action</summary>
        /// <param name="sender"></param> <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Event raised on Valider button click action
        /// </summary>
        /// <param name="sender"></param><param name="e"></param>
        private void btnValide_Click(object sender, EventArgs e)
        {
            if (lbBackupVersion.SelectedIndex >= 0)
            {
                _selectedBackup = (backupVersion)lbBackupVersion.SelectedItem;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void lbBackupVersion_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnValide_Click(sender, EventArgs.Empty);
        }
        #endregion

    }

    #region Helper class

    public partial class backupVersion
    {
        public string Agecny;
        public string Id;
        public string Version;
        public string DateHeure;
        public string ArtefactType;
        public string FullId
        {
            get
            {
                StringBuilder sbFullId = new StringBuilder();
                sbFullId.Append(Agecny).Append("+").Append(Id).Append("+").Append(Version);
                return (sbFullId.ToString());
            }
        }

        public backupVersion()
        {
        }

        public override string ToString()
        {
            StringBuilder sbFormat = new StringBuilder();
            sbFormat.Append("Version ");
            sbFormat.Append(Version);
            sbFormat.Append(" Date ");
            sbFormat.Append(DateHeure);
            return (sbFormat.ToString());

        }
    }
    #endregion
}
